import csv

from selenium import webdriver

# 设置Chrome浏览器启动选项
options = webdriver.ChromeOptions()
options.add_argument('--disable-popup-blocking')
options.add_experimental_option('w3c', False)

# 创建一个空白的浏览器窗口并获取其句柄
driver = webdriver.Chrome(options=options)
main_window = driver.window_handles [ 0 ]

# 打开 CSV 文件并创建 reader 对象
with open('locustmaproot.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)

    # 循环读取每一行
    for row in reader:
        # 打印第一列和第三列数据
        print(row [ 0 ], row [ 2 ])
        ip = row [ 2 ]
        sever = row[0]
        # 打开浏览器窗口并分别填写输入框
        drivers = [ ]

        # 打开Chrome浏览器并打开目标URL
        url = 'http://' + ip + ':8089'
        driver.execute_script("window.open('" + url + "')")
        new_window = driver.window_handles [ -1 ]
        driver.switch_to.window(new_window)

        # 定义要填写的值列表,只打开网页时，注释下面即可
        values = {'user_count': '2000', 'spawn_rate': '40', 'host': sever}  # 根据名称查找每个输入框，并填写对应的值
        for name, value in values.items():
            input_tag = driver.find_element_by_name(name)
            input_tag.clear()
            input_tag.send_keys(value)
        input_tag.submit()
